﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KSSModel;
using Umbraco.Core;
using Umbraco.Core.Models;
using Umbraco.Core.Services;
using umbraco.MacroEngines;
using System.Web.Script.Serialization;
namespace KSSOnline.Controller
{
    public static class TagHelper
    {
        //public static string GetAllTagCate()
        //{
        //    var contentService = ApplicationContext.Current.Services.ContentService;

        //    var ns = contentService.GetChildren(1166);
        //    var serializer = new JavaScriptSerializer();
        //    var aView = serializer.Serialize(ns.ToList());
        //    return aView;
        //}
        #region TagCate
        public static List<tblTagCate> GetAllTagCateOject()
        {
            using (var dataContext = new KSSDBDataContext())
            {
                var serializer = new JavaScriptSerializer();
                try
                {
                    var tagCate = (from tagcate in dataContext.tblTagCates
                                   select tagcate).ToList();

                    return tagCate;
                }
                catch (Exception ex)
                {
                    return null;
                }

            }
        }
        public static string GetAllTagCate()
        {
            using (var dataContext = new KSSDBDataContext())
            {
                var serializer = new JavaScriptSerializer();
                try
                {
                    var tagCate = (from tagcate in dataContext.tblTagCates
                                   select
                                       new
                                           {
                                               tagcate.tagCateId,
                                               tagcate.tagCateName,
                                               tagcate.isDisable,
                                               tagcate.createBy,
                                               tagcate.created
                                           }).ToList();

                    var aView = serializer.Serialize(tagCate);
                    return aView;
                }
                catch (Exception ex)
                {
                    return serializer.Serialize(new Result(false, ex.Message));
                }

            }
        }
        public static string GetAllTagCate(string sort)
        {
            using (var dataContext = new KSSDBDataContext())
            {
                var serializer = new JavaScriptSerializer();
                try
                {
                    var tagCate = (from tagcate in dataContext.tblTagCates
                                   select
                                       new
                                       {
                                           tagcate.tagCateId,
                                           tagcate.tagCateName,
                                           tagcate.isDisable,
                                           tagcate.createBy,
                                           tagcate.created
                                       }).ToList().OrderBy(sort);

                    var aView = serializer.Serialize(tagCate);
                    return aView;

                }
                catch (Exception ex)
                {
                    return serializer.Serialize(new Result(false, ex.Message));
                }

            }
        }

        public static string AddTagCate(tblTagCate newTagCate)
        {
            using (var dataContext = new KSSDBDataContext())
            {
                var serializer = new JavaScriptSerializer();
                try
                {
                    dataContext.tblTagCates.InsertOnSubmit(newTagCate);
                    dataContext.SubmitChanges();
                    KSSMaster.AddLog(new tblLog()
                                         {
                                             details =
                                                 "เพิ่มหมวดหมู่ Tag ชื่อ " + newTagCate.tagCateName,
                                             username = newTagCate.createBy,
                                             logType = 0
                                         });
                }
                catch (Exception ex)
                {
                    KSSMaster.AddLog(new tblLog()
                    {
                        details =
                            ex.Message,
                            username = newTagCate.createBy,
                        logType = 1
                    });
                    return serializer.Serialize(new Result(false, ex.Message));
                }

                return serializer.Serialize(new Result(true));
            }
        }
        public static string EditTagCate(tblTagCate editTagCate)
        {
            using (var dataContext = new KSSDBDataContext())
            {
                var serializer = new JavaScriptSerializer();
                try
                {
                    
                    tblTagCate oldDocument =
                                            dataContext.tblTagCates.Single(tagcate => tagcate.tagCateId == editTagCate.tagCateId);
                    var log = new StringBuilder();
                    if (!string.IsNullOrEmpty(editTagCate.tagCateName) && oldDocument.tagCateName != editTagCate.tagCateName)
                    {
                        oldDocument.tagCateName = editTagCate.tagCateName;
                        log.Append("แก้ไขมวดหมู่ Tag จากชื่อ " + oldDocument.tagCateName + "เป็นชื่อ " + editTagCate.tagCateName);
                    }
                    if (oldDocument.isDisable != editTagCate.isDisable )
                    {
                        oldDocument.isDisable = editTagCate.isDisable;
                        log.Append(" แก้ไขมวดหมู่ Tag ที่ชื่อ " + editTagCate.tagCateName +  "ที่disable เป็น " + editTagCate.isDisable);
                    }

                    dataContext.SubmitChanges();
                    KSSMaster.AddLog(new tblLog()
                    {
                        details =
                            log.ToString(),
                        username = editTagCate.createBy,
                        logType = 0
                    });
                }
                catch (Exception ex)
                {
                    KSSMaster.AddLog(new tblLog()
                    {
                        details =
                            ex.Message,
                        username = editTagCate.createBy,
                        logType = 1
                    });
                    return serializer.Serialize(new Result(false, ex.Message));
                }
                return serializer.Serialize(new Result(true));

            }
        }
        public static string DeleteTagCate(int tagCateID)
        {
            using (var dataContext = new KSSDBDataContext())
            {
                var serializer = new JavaScriptSerializer();
                try
                {
                    tblTagCate oldDocument =
                                            dataContext.tblTagCates.Single(tagcate => tagcate.tagCateId == tagCateID);
                    string deleteTag =
                   "DELETE FROM tblTagCate WHERE tagCateId =" + tagCateID;
                    dataContext.ExecuteCommand(deleteTag);

                    KSSMaster.AddLog(new tblLog()
                    {
                        details =
                            "ลบมวดหมู่ Tag ที่ชื่อ " + oldDocument.tagCateName + " ID= " + tagCateID,
                        username = System.Web.HttpContext.Current.Request.LogonUserIdentity.Name ?? "admin",
                        logType = 0
                    });
                }
                catch (Exception ex)
                {
                    KSSMaster.AddLog(new tblLog()
                    {
                        details =
                            ex.Message,
                        username = System.Web.HttpContext.Current.Request.LogonUserIdentity.Name ?? "admin",
                        logType = 1
                    });
                    return serializer.Serialize(new Result(false, ex.Message));
                }
                return serializer.Serialize(new Result(true));
            }
        }
        #endregion
        //public static string AddTagCate(TagCate newTagCate)
        //{
        //    var serializer = new JavaScriptSerializer();
        //    try
        //    {
        //        var contentService = ApplicationContext.Current.Services.ContentService;
        //        var tagCate = contentService.CreateContent(newTagCate.Name, 1166, "TagsCate");
        //        if (newTagCate.IsDisable)
        //        {
        //            tagCate.SetValue("isDisable", newTagCate.IsDisable);
        //        }
        //        contentService.SaveAndPublish(tagCate);
        //    }

        //    catch (Exception ex)
        //    {
        //        return serializer.Serialize(new Result(false, ex.Message));
        //    }
        //    return serializer.Serialize(new Result(true));
        //}

        //public static string EditTagCate(TagCate editTagCate)
        //{
        //    var serializer = new JavaScriptSerializer();
        //    try
        //    {
        //        var contentService = ApplicationContext.Current.Services.ContentService;
        //        var tagCate = contentService.GetById(editTagCate.tagCateID);
        //        tagCate.Name = editTagCate.Name;
        //        tagCate.SetValue("isDisable", editTagCate.IsDisable);
        //        contentService.SaveAndPublish(tagCate);
        //    }
        //    catch (Exception ex)
        //    {
        //        return serializer.Serialize(new Result(false, ex.Message));
        //    }
        //    return serializer.Serialize(new Result(true));
        //}

        //public static string DeleteTagCate(int tagCateID)
        //{
        //    var serializer = new JavaScriptSerializer();
        //    try
        //    {
        //        var contentService = ApplicationContext.Current.Services.ContentService;
        //        var tagCate = contentService.GetById(tagCateID);
        //        contentService.Delete(tagCate);
        //        return serializer.Serialize(new Result(true));
        //    }
        //    catch (Exception ex)
        //    {
        //        return serializer.Serialize(new Result(false, ex.Message));
        //    }
        //}

        #region Tag
        public static List<tblTag> GetAllTagObject()
        {
            using (var dataContext = new KSSDBDataContext())
            {
                var serializer = new JavaScriptSerializer();
                try
                {
                    var tags = (from tag in dataContext.tblTags
                                select tag ).ToList();

                    return tags;
                }
                catch (Exception ex)
                {
                    return null;
                }

            }
        }
        public static string GetTag()
        {
            using (var dataContext = new KSSDBDataContext())
            {
                var serializer = new JavaScriptSerializer();
                try
                {
                    var tags = (from tag in dataContext.tblTags
                                join tagcate in dataContext.tblTagCates
                                    on tag.tagCateId equals tagcate.tagCateId
                                select
                                    new
                                    {
                                        tag.tagId,
                                        tag.tagCateId,
                                        tagcate.tagCateName,
                                        tag.tagName,
                                        tag.isDisable,
                                        tag.createBy,
                                        tag.created
                                    }).ToList();

                    var aView = serializer.Serialize(tags);
                    return aView;
                }
                catch (Exception ex)
                {
                    return serializer.Serialize(new Result(false, ex.Message));
                }

            }
        }
        public static string GetTag(string sort)
        {
            using (var dataContext = new KSSDBDataContext())
            {
                var serializer = new JavaScriptSerializer();
                try
                {
                    var tags = (from tag in dataContext.tblTags
                                join tagcate in dataContext.tblTagCates
                                    on tag.tagCateId equals tagcate.tagCateId
                                select
                                    new
                                    {
                                        tag.tagId,
                                        tag.tagCateId,
                                        tagcate.tagCateName,
                                        tag.tagName,
                                        tag.isDisable,
                                        tag.createBy,
                                        tag.created
                                    }).ToList().OrderBy(sort); ;

                    var aView = serializer.Serialize(tags);
                    return aView;
                }
                catch (Exception ex)
                {
                    return serializer.Serialize(new Result(false, ex.Message));
                }

            }
        }
        public static string AddTag(tblTag newTag)
        {
            using (var dataContext = new KSSDBDataContext())
            {
                var serializer = new JavaScriptSerializer();
                try
                {
                    dataContext.tblTags.InsertOnSubmit(newTag);
                    dataContext.SubmitChanges();
                    KSSMaster.AddLog(new tblLog()
                    {
                        details =
                            "เพิ่ม ชื่อ " + newTag.tagName,
                        username = newTag.createBy,
                        logType = 0
                    });
                }
                catch (Exception ex)
                {
                    KSSMaster.AddLog(new tblLog()
                    {
                        details =
                            ex.Message,
                        username = newTag.createBy,
                        logType = 1
                    });
                    return serializer.Serialize(new Result(false, ex.Message));
                }

                return serializer.Serialize(new Result(true));
            }
        }
        public static string EditTag(tblTag editTag)
        {
            using (var dataContext = new KSSDBDataContext())
            {
                var serializer = new JavaScriptSerializer();
                try
                {

                    tblTag oldTag =
                                            dataContext.tblTags.Single(tagcate => tagcate.tagId == editTag.tagId);
                    var log = new StringBuilder();
                    if (!string.IsNullOrEmpty(editTag.tagName) && oldTag.tagName != editTag.tagName)
                    {
                        log.Append("แก้ไข Tag จากชื่อ " + oldTag.tagName + " เป็นชื่อ " + editTag.tagName);
                        oldTag.tagName = editTag.tagName;
                        
                    }
                    if (editTag.tagCateId != -1 && oldTag.tagCateId != editTag.tagCateId)
                    {
                        log.Append(" แก้ไขหมวดหมู Tag ID ที่ Tag " + editTag.tagName + " เป็น " + editTag.tagCateId);
                        oldTag.tagCateId = editTag.tagCateId;
                    }
                    if (oldTag.isDisable != editTag.isDisable)
                    {
                        log.Append(" แก้ไข Tag ที่ชื่อ" + editTag.tagName + " ที่disable เป็น " + editTag.isDisable);
                        oldTag.isDisable = editTag.isDisable;
                    }
                    
                    dataContext.SubmitChanges();
                    KSSMaster.AddLog(new tblLog()
                    {
                        details =
                            log.ToString(),
                        username = editTag.createBy,
                        logType = 0
                    });
                }
                catch (Exception ex)
                {
                    KSSMaster.AddLog(new tblLog()
                    {
                        details =
                            ex.Message,
                        username = editTag.createBy,
                        logType = 1
                    });
                    return serializer.Serialize(new Result(false, ex.Message));
                }
                return serializer.Serialize(new Result(true));

            }
        }
        public static string DeleteTag(int tagID)
        {
            using (var dataContext = new KSSDBDataContext())
            {
                var serializer = new JavaScriptSerializer();
                try
                {
                    tblTag oldTag = dataContext.tblTags.Single(tagcate => tagcate.tagId == tagID);
                    string deleteTag =
                    "DELETE FROM tblTag WHERE tagId =" + tagID;
                    dataContext.ExecuteCommand(deleteTag);
                    KSSMaster.AddLog(new tblLog()
                    {
                        details =
                            "ลบ Tag ที่ ชื่อ " + oldTag.tagName+ " ID=" + tagID,
                        username = System.Web.HttpContext.Current.Request.LogonUserIdentity.Name ?? "admin",
                        logType = 0
                    });
                }
                catch (Exception ex)
                {
                    KSSMaster.AddLog(new tblLog()
                    {
                        details =
                            ex.Message,
                        username = System.Web.HttpContext.Current.Request.LogonUserIdentity.Name ?? "admin",
                        logType = 1
                    });
                    return serializer.Serialize(new Result(false, ex.Message));
                }
                return serializer.Serialize(new Result(true));
            }
        }
        #endregion
    }
    [Serializable]
    public class TagCate
    {
        public TagCate(string name, string isDisable)
        {
            Name = name;
            if (!string.IsNullOrEmpty(isDisable))
            {
                if (isDisable == "1" || isDisable == "true")
                {
                    IsDisable = true;
                }
            }
        }
        public TagCate(string name)
        {
            Name = name;
        }
        public TagCate()
        {
        }
        public int tagCateID;
        public string Name;
        public bool IsDisable;
    }

    [Serializable]
    public class Tag
    {
        public Tag(string name, int tagCateID, string tagCateName, string isDisable)
        {
            Name = name;
            TagCateID = tagCateID;
            TagCateName = tagCateName;
            if (!string.IsNullOrEmpty(isDisable))
            {
                if (isDisable == "1" || isDisable == "true")
                {
                    IsDisable = true;
                }
            }
        }
        public Tag(string name)
        {
            Name = name;
        }
        public Tag()
        {
        }
        public int TagCateID;
        public string TagCateName;
        public int TagID;
        public string Name;
        public bool IsDisable;
    }

}
